<?php
	$ls_gridname	= 'dtl_sample2';


// ----------------- >> Form Detail << ----------------- ?>
<script type="text/javascript">

	$(document).ready(function(){
		// Setup Form
		$('#form_<?php echo $ls_gridname; ?>').setupForm([
			{ name: 'keterangan_uraian', type: 'text', size: 'long', maxlength: 255 },
			{ name: 'banyak_satuan', type: 'text', size: 'medium', maxlength: 255 },
			{ name: 'harga_satuan', type: 'text', size: 'medium', maxlength: 40},
			{ name: 'total', type: 'text', size: 'medium', maxlength: 40}
		]);	

		$('#harga_satuan').keyup(function() {
			var banyak_satuan =  parseInt($('#banyak_satuan').val());
			var harga_satuan =  parseInt($('#harga_satuan').val());
			var total = harga_satuan * banyak_satuan;
			$('#total').setval(total);
			$('#terbilang').setval(terbilang(total));
		});		

		$('#form_<?php echo $ls_gridname; ?>_container').dialog({
			closed:true,
			modal:true,
			title: 'Tambah Detail',
			width:550,
			height:250,
			buttons:[{
				text:'Save',
				iconCls:'icon-save',
				handler:function(){
					var ls_mode			= $('#form_<?php echo $ls_gridname; ?>').data('mode');
					var ls_error		= '';
					var row	= formToJSON('form_<?php echo $ls_gridname; ?>');
					console.log(row);
					// $('#form_master_detail').submitForm('/'+controller+'/ajax_post_detail', { onComplete : function(){ $('#dialog_container_detail').dialog('close'); $('#datagrid_detail').datagrid('reload'); }} );
					
					if(row.kode_produk == ''){
						ls_error	+= 'Isi Barang\n';
					}
					if(row.banyak == 0){
						ls_error	+= 'Isi Jumlah Barang\n';
					}
					
					// Eksekusi
					if(ls_error!=''){
						$.messager.alert('Error', nl2br(ls_error), 'error');
					}else{
						if(ls_mode=='create'){
							
							$('#<?php echo $ls_gridname; ?>').datagrid('appendRow', row );
							$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');	
						
						} else {
							var selected = $('#<?php echo $ls_gridname; ?>').datagrid('getSelected');
							var rowIndex = $("#<?php echo $ls_gridname; ?>").datagrid("getRowIndex", selected);

							updateRowFlag('<?php echo $ls_gridname; ?>', row); // Patching Flag Update
							$('#<?php echo $ls_gridname; ?>').datagrid('updateRow',{index:rowIndex,row:row});
							
							$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');
						}
					}
				}
			},{
				text:'Close',
				iconCls:'icon-cancel',
				handler:function(){
					$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');
				}
			}]
		});

	});
</script>


<div id="form_<?php echo $ls_gridname; ?>_container" style="padding:5px;width:800px;height:400px;">
	<div id="form_<?php echo $ls_gridname; ?>">
    
    <table class="tb_form_detail" style="width:100%;" cellpadding="0" cellspacing="0">    
    <tr>
        <td><input id="id_pembayaran_detail" name="id_pembayaran_detail" type="hidden" />
        	<input id="id_pembayaran" name="id_pembayaran" type="hidden" />
        </td>
    </tr>
    <tr>
        <td class="label">Keterangan Uraian</td>
        <td><textarea rows="5" id="keterangan_uraian" name="keterangan_uraian" /></td>
    </tr>
	<tr>
        <td class="label">Banyak Satuan</td>
        <td><input id="banyak_satuan" /></td>
    </tr>
    <tr>
        <td class="label">Harga Satuan</td>
        <td><input id="harga_satuan" /></td>
    </tr>
    <tr>
        <td class="label">Total</td>
        <td><input id="total" /></td>
    </tr>
    </table>
	</div>
</div>

<?php 

// ----------------- >> Table Detail << ----------------- ?>
<script type="text/javascript">
	$(document).ready(function(){
		<?php
			if ($mode == 'edit') {
				?>
					var id_pembayaran_auto = "<?php echo $id; ?>"
					var id_pembayaran_detail_auto = "<?php echo $id_pembayaran_detail_auto; ?>"
				<?php
			}
			
		?>

		<?php
			if ($mode == 'create') {
				?>
					var id_pembayaran_detail_auto = "<?php echo $id_pembayaran_detail_auto; ?>"
					var id_pembayaran_auto = "<?php echo $id_pembayaran_auto; ?>";
					
				<?php
			}
			
		?>
		
		/*var t = parseInt(id_pembayaran_detail_auto) + 1 ;*/

		$('#<?php echo $ls_gridname; ?>').datagrid({
			url:gs_path+'/'+controller+'/<?php echo $ls_gridname; ?>_data',
			width:1024,
			height:250,
			queryParams:{ id_pembayaran:$('#id_pembayaran').getval(), rows:9999999999 },
			<?php if($mode!='view'){ 
			// -->> Toolbar [Start]
			// Only Appear On Mode Create / Edit ?>
			toolbar:[{
				text:'Add',
				iconCls:'icon-add',
				handler:function(){
					//alert(id_pembayaran_auto);
						resetError();
						$('#form_<?php echo $ls_gridname; ?>_container').dialog('open');
						$('#form_<?php echo $ls_gridname; ?>').data('mode', 'create');
						$('#form_<?php echo $ls_gridname; ?>_container').prev().find('.panel-title').html('Tambah Detail Perakitan');					
						$('#form_<?php echo $ls_gridname; ?>').resetForm();
						$('#id_pembayaran_detail').setval(id_pembayaran_detail_auto);
						$('#id_pembayaran').setval(id_pembayaran_auto);
				}
			},'-',{
				text:'Edit',
				iconCls:'icon-edit',
				handler:function(){
					var oSel=$('#<?php echo $ls_gridname; ?>').datagrid('getSelected'); 
					if(oSel==null){ $.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); }
					else{ 
						resetError();
						$('#form_<?php echo $ls_gridname; ?>_container').dialog('open');
						$('#form_<?php echo $ls_gridname; ?>').data('mode', 'edit');
						$('#form_<?php echo $ls_gridname; ?>_container').prev().find('.panel-title').html('Ubah Detail Barang');
						$('#id_pembayaran_detail').setval(oSel['id_pembayaran_detail']);
						$('#id_pembayaran').setval(oSel['id_pembayaran']);
						jsonToForm(oSel);
					}
				}
			},'-',{
				text:'Delete',
				iconCls:'icon-remove',
				handler:function(){
					var row = $('#<?php echo $ls_gridname; ?>').datagrid('getSelected');
					if(row==null){ $.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); }
					else if(row){
						if(!confirm('Hapus baris data?')) return false;

						var index = $('#<?php echo $ls_gridname; ?>').datagrid('getRowIndex', row);
						$('#<?php echo $ls_gridname; ?>').datagrid('deleteRow', index);
					}
				}
			}],<?php 
			} // -->> Toolbar [End]
			?>
			onClickRow:function(rowIndex){
				var lastIndex = $('#<?php echo $ls_gridname; ?>').data('selectedIndex');
				$('#<?php echo $ls_gridname; ?>').data('selectedIndex', rowIndex);
			},
			onLoadSuccess:function(){ }
		});
		
	});
	
</script>


<table id="<?php echo $ls_gridname; ?>" rownumbers="true" style="width:600;height:auto" singleSelect="true" idField="#" showFooter="true">
<thead>
    <tr>
    	<th field="id_pembayaran_detail" width="200">ID Pembayaran Detail</th>
        <th field="keterangan_uraian" width="200">Keterangan Uraian</th>
    	<th field="banyak_satuan" width="200">Banyak Satuan</th>
    	<th field="harga_satuan" width="200">Harga Satuan</th>
    	<th field="total" width="200">Total</th>
    </tr>
</thead>
</table>
